#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "calibration.h"
#include "eeprom.h"
#include "zlog.h"

//从EEPROM中读取加速度各通道校准值
void adc_cali_value_set(sys_cfg_info *cfg, chx_cfg_info chx[])
{
	uint8_t ch;
	char ee_rd_buf[8 + 1];
	char ee_rd_cali[8 + 1];

	float temp_cali = 0.0;

	printf("start obtaining adc calibration value data!\n ");

	//EEPROM获取校准值,出厂时写入EEPROM中
	eeprom_page_read(eeprom_addr0, EE_PROG_MARK_OFFSET, (uint8_t*)ee_rd_buf, 4);
	ee_rd_buf[4] = '\0';
	if (strcmp(ee_rd_buf, "#C0FFEE#") == 0) {
		for (ch = 0; ch < ACC_CH_TOTAL; ch++) {
			eeprom_page_read(eeprom_addr1, EE_CH0_CALIB_VAL_OFFSET + (ch * 0x08), (uint8_t*)ee_rd_cali, 8);
			ee_rd_buf[8] = '\0';
			temp_cali = atof(ee_rd_cali);
			if(temp_cali <= 199.9) {				//不大于100mv才是正确的
				chx[ch].chx_calib = temp_cali;
				if (cfg->debug == ON)
					printf("chx[%d].chx_calib is %8.5f\n", ch, chx[ch].chx_calib);
			} else {
				chx[ch].chx_calib = 0.0;
				printf("chx[%d].chx_calib is %8.5f that is overflow!!!\n", ch, temp_cali);
				dzlog_warn("chx[%d].chx_calib is %8.5f that is overflow!!!", ch, temp_cali);
			}
		}
	} else {
		dzlog_warn("programming mark is error, equipment may not be calibrated!!!");
		printf("programming mark is error, equipment may not be calibrated!!!\n");
		for (ch = 0; ch < ACC_CH_TOTAL; ch++)
			chx[ch].chx_calib = 0.0;
	}
}


